package com.yx.admin;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TableItem;

import java.io.File;
import java.io.FileOutputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class WebServerConfig extends Composite {
	private Text MEN_MIN;
	private Text MEN_MAX;
	private Text XMN;
	private Text XSS;
	private Text MIN_THREAD;
	private Text MAX_THREAD;
	private Text MIN_ASYNC_THREAD;
	private Text MAX_ASYNC_THREAD;
	private Text MAX_UPLOAD;
	private Text MIN_JDBC;
	private Text MAX_JDBC;
	private Combo LOG;
	private Text PORT;
	private Label message;
	private Text JAR;
	

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public WebServerConfig(Composite parent, int style) {
		super(parent, style);
		final Composite window=parent;
		Label label_1 = new Label(this, SWT.NONE);
		label_1.setBounds(240, 150, 61, 17);
		label_1.setText("日志级别：");
		
		LOG = new Combo(this, SWT.NONE);
		LOG.setItems(new String[] {"调试", "信息", "警告", "错误", "致命"});
		LOG.setBounds(303, 146, 61, 25);
		LOG.select(0);
		
		Label label = new Label(this, SWT.NONE);
		label.setText("初始堆内存：");
		label.setBounds(228, 44, 72, 17);
		
		MEN_MIN = new Text(this, SWT.BORDER);
		MEN_MIN.setText("2048");
		MEN_MIN.setBounds(303, 41, 61, 23);
		
		Label label_2 = new Label(this, SWT.NONE);
		label_2.setText("最大堆内存：");
		label_2.setBounds(31, 70, 72, 17);
		
		MEN_MAX = new Text(this, SWT.BORDER);
		MEN_MAX.setText("2048");
		MEN_MAX.setBounds(106, 67, 61, 23);
		
		Label label_3 = new Label(this, SWT.NONE);
		label_3.setText("年轻代内存：");
		label_3.setBounds(228, 70, 72, 17);
		
		XMN = new Text(this, SWT.BORDER);
		XMN.setText("768");
		XMN.setBounds(303, 67, 61, 23);
		
		Label label_4 = new Label(this, SWT.NONE);
		label_4.setText("M");
		label_4.setBounds(370, 70, 17, 17);
		
		Label label_5 = new Label(this, SWT.NONE);
		label_5.setText("M");
		label_5.setBounds(370, 44, 17, 17);
		
		XSS = new Text(this, SWT.BORDER);
		XSS.setText("1024");
		XSS.setBounds(106, 94, 61, 23);
		
		Label label_6 = new Label(this, SWT.NONE);
		label_6.setText("栈内存：");
		label_6.setBounds(54, 97, 49, 17);
		
		Label label_7 = new Label(this, SWT.NONE);
		label_7.setText("K");
		label_7.setBounds(170, 97, 17, 17);
		
		Label label_8 = new Label(this, SWT.NONE);
		label_8.setText("M");
		label_8.setBounds(170, 70, 17, 17);
		
		Label label_9 = new Label(this, SWT.NONE);
		label_9.setText("WEB最小线程：");
		label_9.setBounds(212, 97, 88, 17);
		
		MIN_THREAD = new Text(this, SWT.BORDER);
		MIN_THREAD.setText("20");
		MIN_THREAD.setBounds(303, 94, 61, 23);
		
		Label label_10 = new Label(this, SWT.NONE);
		label_10.setText("WEB最大线程：");
		label_10.setBounds(15, 123, 88, 17);
		
		MAX_THREAD = new Text(this, SWT.BORDER);
		MAX_THREAD.setText("100");
		MAX_THREAD.setBounds(106, 120, 61, 23);
		
		Label label_11 = new Label(this, SWT.NONE);
		label_11.setText("异步最小线程：");
		label_11.setBounds(216, 123, 84, 17);
		
		MIN_ASYNC_THREAD = new Text(this, SWT.BORDER);
		MIN_ASYNC_THREAD.setText("20");
		MIN_ASYNC_THREAD.setBounds(303, 120, 61, 23);
		
		Label label_12 = new Label(this, SWT.NONE);
		label_12.setText("异步最大线程：");
		label_12.setBounds(18, 150, 83, 17);
		
		MAX_ASYNC_THREAD = new Text(this, SWT.BORDER);
		MAX_ASYNC_THREAD.setText("100");
		MAX_ASYNC_THREAD.setBounds(106, 147, 61, 23);
		
		Label label_13 = new Label(this, SWT.NONE);
		label_13.setText("最大上传文件：");
		label_13.setBounds(18, 176, 84, 17);
		
		MAX_UPLOAD = new Text(this, SWT.BORDER);
		MAX_UPLOAD.setText("40");
		MAX_UPLOAD.setBounds(106, 173, 61, 23);
		
		Label label_14 = new Label(this, SWT.NONE);
		label_14.setText("M");
		label_14.setBounds(170, 176, 17, 17);
		
		Label label_15 = new Label(this, SWT.NONE);
		label_15.setText("最小数据库连接：");
		label_15.setBounds(204, 176, 96, 17);
		
		MIN_JDBC = new Text(this, SWT.BORDER);
		MIN_JDBC.setText("15");
		MIN_JDBC.setBounds(303, 173, 61, 23);
		
		Label label_16 = new Label(this, SWT.NONE);
		label_16.setText("最大数据库连接：");
		label_16.setBounds(6, 203, 96, 17);
		
		MAX_JDBC = new Text(this, SWT.BORDER);
		MAX_JDBC.setText("60");
		MAX_JDBC.setBounds(106, 200, 61, 23);
		
		Button button = new Button(this, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				try{
					int port=getInt(PORT);
					int men_min=getInt(MEN_MIN);
					int men_max=getInt(MEN_MAX);
					int xmn=getInt(XMN);
					int xss=getInt(XSS);
					int MinThreadPool=getInt(MIN_THREAD);
					int MaxThreadPool=getInt(MAX_THREAD);
					int MinAsyncThreadPool=getInt(MIN_ASYNC_THREAD);
					int MaxAsyncThreadPool=getInt(MAX_ASYNC_THREAD);
					int UploadFile=getInt(MAX_UPLOAD);
					int MinJdbcPool=getInt(MIN_JDBC);
					int MaxJdbcPool=getInt(MAX_JDBC);
					int LogLevel= LOG.getSelectionIndex();
					String command="java -server -Xms"+men_min+"m -Xmx"+men_max+"m -Xmn"+xmn+"m -Xss"+xss+"k -Dfile.encoding=\"UTF-8\" -cp ServiceBuilder.jar;"+JAR.getText()+" builder.Starter ";
					command=command+"Port="+port+" LogLevel="+LogLevel+" MinThreadPool="+MinThreadPool+" MaxThreadPool="+MaxThreadPool;
					command=command+" MinAsyncThreadPool="+MinAsyncThreadPool+" MaxAsyncThreadPool="+MaxAsyncThreadPool;
					command=command+" UploadFile="+UploadFile+" MinJdbcPool="+MinJdbcPool+" MaxJdbcPool="+MaxJdbcPool;
					File file=new File("./starter.bat");
					if(file.exists())file.delete();
					FileOutputStream out=new  FileOutputStream(file,false);
					out.write(command.getBytes());
					out.flush();
					out.close();
					message.setText("启动文件生成成功，文件名：StorageStart.bat！");
				}catch(Exception ex){
					return;
				}
			}
		});
		button.setText("生成启动文件");
		button.setBounds(260, 264, 104, 27);
		
		Label label_17 = new Label(this, SWT.NONE);
		label_17.setText("服务器端口：");
		label_17.setBounds(31, 44, 72, 17);
		
		PORT = new Text(this, SWT.BORDER);
		PORT.setText("8080");
		PORT.setBounds(106, 41, 61, 23);
		
		message = new Label(this, SWT.NONE);
		message.setBounds(6, 237, 358, 17);
		
		Label lbljar = new Label(this, SWT.NONE);
		lbljar.setText("应用JAR文件名：");
		lbljar.setBounds(10, 17, 93, 17);
		
		JAR = new Text(this, SWT.BORDER);
		JAR.setEditable(false);
		JAR.setBounds(106, 14, 189, 23);
		
		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				FileDialog filedlg = new FileDialog(window.getShell(), SWT.OPEN);
				filedlg.setText("请选择服务器列表清单文件：");
				filedlg.setFilterPath("./");
				String selected = filedlg.open();
				if(selected!=null){
					JAR.setText(selected);
				}
			}
		});
		btnNewButton.setBounds(303, 12, 61, 27);
		btnNewButton.setText("选择");

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	private int getInt(Text com)throws Exception{
		String vStr=com.getText().trim();
		try{
			return Integer.parseInt(vStr);
		}catch(Exception e){
			com.setText("");
			com.setFocus();
			message.setText("输入错误，输入整数数字！");
			throw e;
		}
	}
}
